package cc.dawning.utiming.lklocation.location.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;

import java.util.ArrayList;
import java.util.HashMap;

import cc.dawning.utiming.lklocation.R;


/**
 * Date 2018/3/27
 * Describe 推荐地址适配器
 *
 * @auther liuxin
 */

public class LocationAdapter extends BaseAdapter {

    /**
     * 数据源
     */
    private ArrayList<PoiItem> mObjList;

    private Activity mActivity;

    public HashMap<Integer, Boolean> isSelected = new HashMap<>();

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }

    public LocationAdapter(ArrayList<PoiItem> objList, Activity activity) {
        this.mObjList = objList;
        this.mActivity = activity;
        for (int i = 0; i < 50; i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public int getCount() {
        return mObjList.size();
    }

    @Override
    public PoiItem getItem(int position) {
        return mObjList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LocationViewHolder locationViewHolder;
        if (convertView == null) {
            convertView = View.inflate(mActivity, R.layout.adapter_location, null);
            locationViewHolder = new LocationViewHolder(convertView);
            convertView.setTag(locationViewHolder);
        } else {
            locationViewHolder = (LocationViewHolder) convertView.getTag();
        }

        try {
            PoiItem p = mObjList.get(position);
            //选择标记
            if (getIsSelected().get(position)) {
                locationViewHolder.Iv_check.setVisibility(View.VISIBLE);
            } else {
                locationViewHolder.Iv_check.setVisibility(View.GONE);
            }
            //列表显示
//            if (position == 0) {
//                locationViewHolder.tv_location.setText("[位置]");
//                locationViewHolder.tv_location_detail.setText(p.getDirection());
//            } else {
            locationViewHolder.tv_location.setText(p + "");
            locationViewHolder.tv_location_detail.setText(p.getProvinceName() + p.getCityName() + p.getAdName() + p.getSnippet());
//            }
        } catch (Exception e) {
            Log.e("location", e.getMessage());
        }

        return convertView;
    }

    public static class LocationViewHolder {
        public View rootView;
        public ImageView Iv_check;
        public TextView tv_location;
        public TextView tv_location_detail;
        public RelativeLayout Rel_top;

        public LocationViewHolder(View rootView) {
            this.rootView = rootView;
            this.Iv_check = rootView.findViewById(R.id.Iv_check);
            this.tv_location = rootView.findViewById(R.id.tv_location);
            this.tv_location_detail = rootView.findViewById(R.id.tv_location_detail);
            this.Rel_top = rootView.findViewById(R.id.Rel_top);
        }

    }
}
